package ywpt.web.common;

import java.net.InetAddress;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import ywpt.components.acegi.domain.Operation;

public class PublicFunction {
	/**
	 * 获得新的流水号(最后是两位：....01)
	 * @param headWord 前缀
	 * @param num 号码
	 * @param isDate 是否加入时间
	 * @return(AB2007101001)
	 */
	public static String getNextCode(String headWord, Integer totalRows,boolean isDate)
	{
		String newCode=""+headWord;
		if(isDate)
		{
			SimpleDateFormat dateformat=new SimpleDateFormat("yyyyMMdd");
	        String now=dateformat.format(new Date());
			newCode=headWord+now;
		}
		
		Integer num=totalRows+1;
		if(num<10)
		{
			newCode=newCode+"0"+num.toString();
		}else
		{
			newCode=newCode+num.toString();
		}
		return newCode;
	}
	/**
	 * 
	 * @param headWord
	 * @param totalRows
	 * @param isDate
	 * @param size 号码的格式位数
	 * @return
	 */
	public static String getNextCode(String headWord, Integer totalRows,boolean isDate,int size)
	{
		String newCode=""+headWord;
		String format="";
		if(isDate)
		{
			SimpleDateFormat dateformat=new SimpleDateFormat("yyyyMMdd");
	        String now=dateformat.format(new Date());
			newCode=headWord+now;
		}
		
		Integer num=totalRows+1;
		for(int i=0;i<size-num.toString().length();i++)
		{
			format+="0";
		}
		
		newCode=newCode+format+num.toString();
		
		return newCode;
	}
	/**
	 * 由开始号码和数量生成结束号码
	 * @param beginCode 开始号码
	 * @param num       数量
	 * @param headWord  前缀
	 * @param size      位数
	 * @return          结束号码
	 */
	public static String getEndBillCode(String beginCode,Integer num,String headWord,Integer size)
	{
		String endCode=headWord;
		int i=headWord.length();//前缀位数
		Integer beginNo=Integer.valueOf(endCode.substring(i));
		Integer endNo=beginNo+num-1;
		String endWord=endNo.toString();
		int j=endWord.length();
		endCode=beginCode.substring(0, size-j)+endWord;
		return endCode;
	}
	/**
	 * 获得当前年份
	 * @return
	 */
   public static String getNowYear()
   {
	   SimpleDateFormat dateformat=new SimpleDateFormat("yyyyMMdd");
       String now=dateformat.format(new Date());
	   return now.substring(0, 4);
   }
   /**
    * 获得当前月份
    * @return
    */
   public static String getNowMonth()
   {
	   SimpleDateFormat dateformat=new SimpleDateFormat("yyyyMMdd");
       String now=dateformat.format(new Date());
	   return now.substring(4, 2);
   }
   /**
    * 获得当前日期
    * @return
    */
   public static String getNowDate()
   {
	   SimpleDateFormat dateformat=new SimpleDateFormat("yyyyMMdd");
       String now=dateformat.format(new Date());
	   return now.substring(6, 2);
   }
   /**
    * 格式化字符串
    * @param str 
    * @param size 补充的位数
    * @param arg 补充的字符
    * @param loc 补充的方向 0：左边，1：右边
    * @return
    */
   public static String format(String str,int size,String arg,int loc)
   {
	   
	   int length=str.length();
	   String format="";
	   for(int i=0;i<size-length;i++)
	   {
		   format+=arg;
	   }
	   if(loc==0)
	   {
		   str=format+str;
	   }else
	   {
		   str=str+format;
	   }
		   
	   return str;
   }
   
   public static void getOperationPopedom(HashMap<String,Operation> oper,boolean[] pope,String moduleId)
   {
	   for(int i=0;i<15;i++)
	   {
		   String operId=moduleId+i;
		   
		   if(oper.get(operId)!=null)
			   pope[i]=true;
		   else
			   pope[i]=false;
	   }
   }
   
	/*
	 * 给指定日期增加天数
	 */
	public static Date addDate(Date date, long addDay) throws ParseException {  
       long time = date.getTime();  
       addDay = addDay * 24 * 60 * 60 * 1000;  
       time += addDay;  
       return new Date(time);  
	}
	/*
	 *返回的ip是外网客户端的公网ip地址 
	 */
	 public static String getIpAddr(HttpServletRequest request) {
	     String clientIp = request.getHeader("x-forwarded-for");
	     if(clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
	    	 clientIp = request.getHeader("Proxy-Client-IP");
	     }
	     if(clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
	    	 clientIp = request.getHeader("WL-Proxy-Client-IP");
	     }
	     if(clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
	    	 clientIp = request.getRemoteAddr();
	     }
	     
	     return clientIp;
	 }
	 
	 /*
	  * 根据域名获取相应的Ip
	  */
	 public static String  getIpByDomainName(String url) throws Exception {
		return InetAddress.getByName(url).toString().substring(url.length()+1);
	 }
}
